package com.dsteiner.widgets.client;

import com.dsteiner.widgets.client.Widgets.AbsolutePanelExt;
import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.MediaPicture;
import com.dsteiner.widgets.client.controller.PrintableImageController;
import com.dsteiner.widgets.client.model.PrintableImageModel;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ErrorHandler;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseWheelListener;
import com.google.gwt.user.client.ui.Widget;

public class PrintableImage extends LazyWidget implements IImage{

	private PrintableImageController printableImageController = new PrintableImageController();
	private final PrintableImageModel printableImageModel;
	private Image image = new Image();

	public PrintableImage(PrintableImageModel printableImageModel) {
		this.printableImageModel = printableImageModel;
	}

	public int getHeight() {
		return image.getHeight();
	}

	public Image getImage() {
		return image;
	}

	public MediaPicture getMediaPicture() {
		return null;
	}

	public String getUrl() {
		return image.getUrl();
	}

	public int getWidth() {
		return image.getWidth();
	}

	public void setMediaPicture(MediaPicture mediaPicture,
			boolean showLowResolution) {
		image.setUrl(showLowResolution ? mediaPicture.getLowResImage() : mediaPicture.getHighResImage());
	}

	public void setUrl(String url) {
		image.setUrl(url);
	}

	public void addClickListener(ClickListener listener) {
		image.addClickListener(listener);
	}

	public void removeClickListener(ClickListener listener) {
		image.removeClickListener(listener);
	}

	public void addLoadListener(LoadListener listener) {
		image.addLoadListener(listener);
	}

	public void removeLoadListener(LoadListener listener) {
		image.removeLoadListener(listener);
	}

	public void addMouseListener(MouseListener listener) {
		image.addMouseListener(listener);
	}

	public void removeMouseListener(MouseListener listener) {
		image.removeMouseListener(listener);
	}

	public void addMouseWheelListener(MouseWheelListener listener) {
		image.addMouseWheelListener(listener);
	}

	public void removeMouseWheelListener(MouseWheelListener listener) {
		image.removeMouseWheelListener(listener);
	}
	
	public HandlerRegistration addLoadHandler(LoadHandler handler) {
		return image.addLoadHandler(handler);
	}
	public HandlerRegistration addErrorHandler(ErrorHandler handler) {
		return image.addErrorHandler(handler);
	}
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return image.addClickHandler(handler);
	}
	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return image.addMouseDownHandler(handler);
	}
	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return image.addMouseUpHandler(handler);
	}
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return image.addMouseOutHandler(handler);
	}
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return image.addMouseOverHandler(handler);
	}
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return image.addMouseMoveHandler(handler);
	}
	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return image.addMouseWheelHandler(handler);
	}

	@Override
	public Widget createContentWidget() {
		AbsolutePanelExt absolutePanel = new AbsolutePanelExt();
		ImageExt printImage = new ImageExt();
		printImage.setVisible(false);

    	absolutePanel.add(image, 0, 0);
    	absolutePanel.add((Widget)printImage, 0, 0);
    	
    	setStyleName("printable-img");
    	image.addStyleName("screen-img");
    	printImage.addStyleName("print-img");
    	
    	printableImageController.registerPrintImage(printImage);
    	printableImageController.registerPrintableImageModel(printableImageModel);
    	printableImageController.registerPrintableImage(this);
    	printableImageController.registerImage(image);
    	printableImageController.registerAbsolutePanel(absolutePanel);
		return absolutePanel;
	}
	
}